/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.convert.ConversionUnsupportedException;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Formatter
/*     */ {
/*  58 */   static final Set<MailItem.Type> SEARCH_FOR_EVERYTHING = EnumSet.of(MailItem.Type.APPOINTMENT, new MailItem.Type[] { MailItem.Type.CONTACT, MailItem.Type.DOCUMENT, MailItem.Type.MESSAGE, MailItem.Type.NOTE, MailItem.Type.TASK, MailItem.Type.WIKI });
/*     */   
/*     */ 
/*     */   protected static final int TIME_UNSPECIFIED = -1;
/*     */   
/*     */ 
/*     */   public static final String QP_CALLBACK = "callback";
/*     */   private final Pattern ALLOWED_CALLBACK_CHARS;
/*     */   
/*  67 */   public Formatter() { this.ALLOWED_CALLBACK_CHARS = Pattern.compile("^[a-zA-Z0-9_.-]+$"); }
/*     */   public abstract FormatterFactory.FormatType getType();
/*  69 */   private static String PROGRESS = "-progress";
/*     */   
/*     */   public String[] getDefaultMimeTypes() {
/*  72 */     return new String[0]; }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean requiresAuth()
/*     */   {
/*  79 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public long getDefaultStartTime()
/*     */   {
/*  86 */     return -1L;
/*     */   }
/*     */   
/*     */   public long getDefaultEndTime() {
/*  90 */     return -1L;
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getDefaultSearchTypes() {
/*  94 */     return EnumSet.of(MailItem.Type.MESSAGE);
/*     */   }
/*     */   
/*  97 */   private static Map<Class<? extends Formatter>, FormatListener> listeners = new ConcurrentHashMap();
/*     */   
/*     */   public static void registerListener(Class<? extends Formatter> clazz, FormatListener listener) {
/* 100 */     listeners.put(clazz, listener);
/*     */   }
/*     */   
/*     */   private Set<FormatListener> getClassListeners() {
/* 104 */     Set<FormatListener> set = new HashSet();
/* 105 */     for (Class<? extends Formatter> clazz : listeners.keySet()) {
/* 106 */       if (clazz.isAssignableFrom(getClass())) {
/* 107 */         set.add(listeners.get(clazz));
/*     */       }
/*     */     }
/* 110 */     return set;
/*     */   }
/*     */   
/*     */   private void formatStarted(UserServletContext context) throws ServiceException {
/* 114 */     for (FormatListener listener : getClassListeners()) {
/* 115 */       listener.formatCallbackStarted(context);
/*     */     }
/*     */   }
/*     */   
/*     */   private void formatEnded(UserServletContext context) throws ServiceException {
/* 120 */     for (FormatListener listener : getClassListeners()) {
/* 121 */       listener.formatCallbackEnded(context);
/*     */     }
/*     */   }
/*     */   
/*     */   private void saveStarted(UserServletContext context) throws ServiceException {
/* 126 */     for (FormatListener listener : getClassListeners()) {
/* 127 */       listener.saveCallbackStarted(context);
/*     */     }
/*     */   }
/*     */   
/*     */   private void saveEnded(UserServletContext context) throws ServiceException {
/* 132 */     for (FormatListener listener : getClassListeners()) {
/* 133 */       listener.saveCallbackEnded(context);
/*     */     }
/*     */   }
/*     */   
/*     */   public final void format(UserServletContext context) throws UserServletException, IOException, ServletException, ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 141 */       formatStarted(context);
/* 142 */       formatCallback(context);
/* 143 */       updateClient(context, null);
/*     */     } catch (Exception e) {
/* 145 */       updateClient(context, e);
/*     */     } finally {
/* 147 */       formatEnded(context);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public final void save(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws UserServletException, IOException, ServletException, ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 157 */       saveStarted(context);
/* 158 */       saveCallback(context, contentType, folder, filename);
/* 159 */       updateClient(context, null);
/*     */     } catch (UserServletException e) {
/* 161 */       throw new UserServletException(e.getHttpStatusCode(), e.getMessage(), e);
/*     */     } catch (Exception e) {
/* 163 */       updateClient(context, e);
/* 164 */       saveEnded(context);
/*     */     }
/*     */   }
/*     */   
/*     */   public abstract void formatCallback(UserServletContext paramUserServletContext)
/*     */     throws UserServletException, ServiceException, IOException, ServletException, MessagingException;
/*     */   
/*     */   public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws UserServletException, ServiceException, IOException, ServletException
/*     */   {
/* 174 */     throw new UserServletException(400, "format not supported for save");
/*     */   }
/*     */   
/*     */   public boolean supportsSave() {
/* 178 */     return false;
/*     */   }
/*     */   
/*     */   public Iterator<? extends MailItem> getMailItems(UserServletContext context, long startTime, long endTime, long chunkSize)
/*     */     throws ServiceException
/*     */   {
/* 184 */     if (context.requestedItems != null) {
/* 185 */       return context.getRequestedItems();
/*     */     }
/*     */     
/* 188 */     assert (context.target != null);
/* 189 */     String query = context.getQueryString();
/* 190 */     if (query != null) {
/* 191 */       if ((context.target instanceof Folder)) {
/* 192 */         Folder f = (Folder)context.target;
/* 193 */         if (f.getId() != 1)
/* 194 */           query = "in:" + f.getPath() + " " + query;
/*     */       }
/*     */       Set<MailItem.Type> types;
/*     */       Set<MailItem.Type> types;
/* 198 */       if (context.getTypesString() == null) {
/* 199 */         types = getDefaultSearchTypes();
/*     */       } else {
/*     */         try {
/* 202 */           types = MailItem.Type.setOf(context.getTypesString());
/*     */         } catch (IllegalArgumentException e) {
/* 204 */           throw MailServiceException.INVALID_TYPE(e.getMessage());
/*     */         }
/*     */       }
/* 207 */       ZimbraQueryResults results = context.targetMailbox.index.search(context.opContext, query, types, context.sortBy, context.getOffset() + context.getLimit());
/*     */       
/* 209 */       return new QueryResultIterator(results); }
/* 210 */     if ((context.target instanceof Folder)) {
/* 211 */       Collection<? extends MailItem> items = getMailItemsFromFolder(context, (Folder)context.target, startTime, endTime, chunkSize);
/* 212 */       return items != null ? items.iterator() : null;
/*     */     }
/* 214 */     ArrayList<MailItem> result = new ArrayList();
/* 215 */     result.add(context.target);
/* 216 */     return result.iterator();
/*     */   }
/*     */   
/*     */   public void validateParams(UserServletContext context)
/*     */     throws UserServletException
/*     */   {
/* 222 */     String callback = (String)context.params.get("callback");
/* 223 */     if ((callback != null) && (callback.length() != 0) && 
/* 224 */       (!this.ALLOWED_CALLBACK_CHARS.matcher(callback).matches())) {
/* 225 */       throw UserServletException.badRequest("invalid callback");
/*     */     }
/*     */   }
/*     */   
/*     */   protected Collection<? extends MailItem> getMailItemsFromFolder(UserServletContext context, Folder folder, long startTime, long endTime, long chunkSize)
/*     */     throws ServiceException
/*     */   {
/* 232 */     switch (folder.getDefaultView()) {
/*     */     case APPOINTMENT: 
/*     */     case TASK: 
/* 235 */       return context.targetMailbox.getCalendarItemsForRange(context.opContext, startTime, endTime, folder.getId(), null);
/*     */     case CONTACT: 
/* 237 */       return context.targetMailbox.getContactList(context.opContext, folder.getId(), SortBy.NAME_ASC);
/*     */     case DOCUMENT: 
/*     */     case WIKI: 
/* 240 */       return context.targetMailbox.getDocumentList(context.opContext, folder.getId(), SortBy.NAME_ASC);
/*     */     }
/* 242 */     return context.targetMailbox.getItemList(context.opContext, MailItem.Type.MESSAGE, folder.getId(), context.sortBy);
/*     */   }
/*     */   
/*     */   public static boolean checkGlobalOverride(String attr, Account account) throws ServletException
/*     */   {
/* 247 */     Provisioning prov = Provisioning.getInstance();
/*     */     try {
/* 249 */       return (prov.getConfig().getBooleanAttr(attr, false)) || (account.getBooleanAttr(attr, false));
/*     */     } catch (ServiceException e) {
/* 251 */       throw new ServletException(e);
/*     */     }
/*     */   }
/*     */   
/*     */   protected static class QueryResultIterator implements Iterator<MailItem> {
/*     */     private ZimbraQueryResults results;
/*     */     
/*     */     QueryResultIterator(ZimbraQueryResults results) {
/* 259 */       this.results = results;
/*     */     }
/*     */     
/*     */     public boolean hasNext()
/*     */     {
/* 264 */       if (this.results == null) {
/* 265 */         return false;
/*     */       }
/*     */       try {
/* 268 */         return this.results.hasNext();
/*     */       } catch (ServiceException e) {
/* 270 */         ZimbraLog.misc.warn("caught exception", e); }
/* 271 */       return false;
/*     */     }
/*     */     
/*     */ 
/*     */     public MailItem next()
/*     */     {
/* 277 */       if (this.results == null) {
/* 278 */         return null;
/*     */       }
/*     */       try {
/* 281 */         ZimbraHit hit = this.results.getNext();
/* 282 */         if (hit != null) {
/* 283 */           return hit.getMailItem();
/*     */         }
/*     */       } catch (ServiceException e) {
/* 286 */         ZimbraLog.misc.warn("caught exception", e);
/*     */       }
/* 288 */       return null;
/*     */     }
/*     */     
/*     */     public void remove()
/*     */     {
/* 293 */       throw new UnsupportedOperationException();
/*     */     }
/*     */     
/*     */     public void finished() {
/* 297 */       Closeables.closeQuietly(this.results);
/* 298 */       this.results = null;
/*     */     }
/*     */   }
/*     */   
/*     */   protected PrintWriter updateClient(UserServletContext context, boolean flush) throws IOException
/*     */   {
/*     */     PrintWriter pw;
/* 305 */     if (context.params.get(PROGRESS) == null) {
/* 306 */       context.resp.reset();
/* 307 */       context.resp.setContentType("text/html; charset=\"utf-8\"");
/* 308 */       context.resp.setCharacterEncoding("utf-8");
/* 309 */       context.params.put(PROGRESS, "1");
/* 310 */       PrintWriter pw = context.resp.getWriter();
/* 311 */       pw.print("<html>\n<head>\n</head>\n");
/*     */     } else {
/* 313 */       pw = context.resp.getWriter();
/* 314 */       pw.println();
/*     */     }
/* 316 */     if (flush)
/* 317 */       pw.flush();
/* 318 */     return pw;
/*     */   }
/*     */   
/*     */   protected void updateClient(UserServletContext context, Exception e)
/*     */     throws UserServletException, IOException, ServletException, ServiceException
/*     */   {
/* 324 */     updateClient(context, e, null);
/*     */   }
/*     */   
/*     */   protected void updateClient(UserServletContext context, Exception e, List<ServiceException> w) throws UserServletException, IOException, ServletException, ServiceException
/*     */   {
/* 329 */     String callback = (String)context.params.get("callback");
/* 330 */     Throwable exception = null;
/* 331 */     PrintWriter out = null;
/*     */     
/* 333 */     if (e != null) {
/* 334 */       Throwable cause = e.getCause();
/*     */       
/* 336 */       exception = ((cause instanceof UserServletException)) || ((cause instanceof ServletException)) || ((cause instanceof IOException)) ? cause : e;
/*     */       
/*     */ 
/* 339 */       context.logError(exception);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 344 */     if (((exception instanceof ConversionUnsupportedException)) && ((callback == null) || (!callback.startsWith("ZmPreviewView._errorCallback")))) {
/* 345 */       exception = null;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 352 */     validateParams(context);
/* 353 */     if ((callback == null) || (callback.equals(""))) {
/* 354 */       if (context.params.get(PROGRESS) == null) {
/* 355 */         if (exception == null)
/* 356 */           return;
/* 357 */         if ((exception instanceof UserServletException))
/* 358 */           throw ((UserServletException)exception);
/* 359 */         if ((exception instanceof ServletException))
/* 360 */           throw ((ServletException)exception);
/* 361 */         if ((exception instanceof IOException))
/* 362 */           throw ((IOException)exception);
/* 363 */         if ((exception instanceof MailServiceException.NoSuchItemException))
/* 364 */           throw ((ServiceException)exception);
/* 365 */         if ((exception instanceof ServiceException)) {
/* 366 */           ServiceException se = (ServiceException)exception;
/* 367 */           if (se.getCode() == "service.INVALID_REQUEST") {
/* 368 */             throw se;
/*     */           }
/*     */         }
/* 371 */         throw ServiceException.FAILURE(getType() + " formatter failure", exception);
/*     */       }
/*     */       try {
/* 374 */         out = updateClient(context, false);
/*     */       } catch (IllegalStateException ise) {
/* 376 */         ZimbraLog.misc.warn("format output has already been written.");
/* 377 */         return;
/*     */       }
/* 379 */       if ((exception == null) && ((w == null) || (w.isEmpty()))) {
/* 380 */         out.println("<body></body>\n</html>");
/*     */       } else {
/* 382 */         ZimbraLog.misc.warn(getType() + " formatter exception", exception);
/*     */         
/* 384 */         out.println("<body>\n<pre>");
/* 385 */         if (exception != null)
/* 386 */           out.print(exception.getLocalizedMessage());
/* 387 */         for (ServiceException warning : w) {
/* 388 */           out.println("<br>");
/* 389 */           out.println(warning.toString().replace("\n", "<br>"));
/*     */         }
/* 391 */         out.println("</pre>\n</body>\n</html>");
/*     */       }
/* 393 */     } else if (!"2".equals(context.params.get(PROGRESS))) { String result;
/*     */       String result;
/* 395 */       if (exception != null) {
/* 396 */         if ((exception instanceof ConversionUnsupportedException)) {
/* 397 */           ZimbraLog.misc.warn(getType() + " formatter exception, " + exception.getMessage());
/*     */         } else {
/* 399 */           ZimbraLog.misc.warn(getType() + " formatter exception", exception);
/*     */         }
/* 401 */         result = "fail"; } else { String result;
/* 402 */         if ((w == null) || (w.size() == 0)) {
/* 403 */           if (context.req.getMethod().equals("GET")) {
/* 404 */             return;
/*     */           }
/* 406 */           result = "success";
/*     */         } else {
/* 408 */           result = "warn";
/*     */         }
/*     */       }
/* 411 */       try { out = updateClient(context, false);
/*     */       } catch (IllegalStateException ise) {
/* 413 */         ZimbraLog.misc.warn("format output has already been written.");
/* 414 */         return;
/*     */       }
/*     */       
/* 417 */       context.params.put(PROGRESS, "2");
/* 418 */       out.println("<body onload='onLoad()'>");
/* 419 */       out.println("<script>");
/* 420 */       out.println("function onLoad() {");
/*     */       
/* 422 */       out.print("    window.parent." + callback + "('" + result + "'");
/* 423 */       if (exception != null) {
/* 424 */         ServiceException se = (exception instanceof ServiceException) ? (ServiceException)exception : FormatterServiceException.UNKNOWN_ERROR(exception);
/*     */         
/*     */ 
/*     */ 
/* 428 */         out.print(",\n\t");
/* 429 */         out.print(SoapProtocol.SoapJS.soapFault(se));
/*     */       }
/* 431 */       if (w != null) {
/* 432 */         for (ServiceException warning : w) {
/* 433 */           out.print(",\n\t");
/* 434 */           out.print(SoapProtocol.SoapJS.soapFault(warning));
/*     */         }
/*     */       }
/* 437 */       out.println(");");
/* 438 */       out.println("}");
/* 439 */       out.println("</script>");
/* 440 */       out.println("</body>");
/* 441 */       out.println("</html>");
/*     */     }
/*     */   }
/*     */   
/*     */   protected String getContentType(UserServletContext context, String fallback) {
/* 446 */     String mime = context.req.getParameter("mime");
/* 447 */     if ((mime != null) && (!mime.equals("")))
/* 448 */       return mime;
/* 449 */     if ((fallback != null) && (!fallback.equals(""))) {
/* 450 */       return fallback;
/*     */     }
/* 452 */     return "text/plain";
/*     */   }
/*     */   
/*     */   protected boolean mayAttach(UserServletContext context)
/*     */   {
/* 457 */     String noAttach = context.req.getParameter("noAttach");
/* 458 */     return (noAttach == null) || ((!noAttach.equals("1")) && (!noAttach.equalsIgnoreCase("true")));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/Formatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */